package net.nagaseyasuhito.kinsenka.wicket.page;

import net.nagaseyasuhito.kinsenka.persist.dao.TaskDao;
import net.nagaseyasuhito.kinsenka.persist.entity.Task;
import net.nagaseyasuhito.kinsenka.wicket.component.DetailField;
import net.nagaseyasuhito.kinsenka.wicket.component.NameField;

import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;

import com.google.inject.Inject;

@AuthorizeInstantiation(Roles.USER)
public class ModifyTaskPage extends BasePage {
	private static final long serialVersionUID = 1L;

	private Task task;

	@Inject
	private TaskDao taskDao;

	public ModifyTaskPage(Task task) {
		this.task = task;

		Form<ModifyTaskPage> modifyForm = new Form<ModifyTaskPage>("modifyForm", new CompoundPropertyModel<ModifyTaskPage>(this));
		modifyForm.add(new FeedbackPanel("feedback"));
		modifyForm.add(new NameField("task.subject"));
		modifyForm.add(new DetailField("task.detail"));
		modifyForm.add(new Button("register") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				ModifyTaskPage.this.taskDao.persistOrMerge(ModifyTaskPage.this.task);
				this.setResponsePage(IndexPage.class);
			}
		});

		this.add(modifyForm);
	}
}
